package sweepmine;

import javax.swing.*;
import java.io.*;
import java.net.*;

public class Client {
    private Socket socket;
    private BufferedReader input;
    private PrintWriter output;
    private boolean waiting;
    private boolean waitingArray;
    private String serverReply;
    private String sendMsg;
    private String recMsg;
    private int[][] mine;
    private int row, column;
    private JFrame jf;

    public Client(JFrame jf, String ini, String address, int port, int row, int column) throws IOException {
        this.jf = jf;
        socket = new Socket(address, port);
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        output = new PrintWriter(socket.getOutputStream(), true);
        waiting = true;
        waitingArray = true;
        recMsg = "";
        this.column = column;
        this.row = row;
        output.println(ini);
    }

    // 向服务器申请房间并等待服务器回复
    public void start() {
        new Thread(() -> {
            try {
                while (waiting) {
                    String message = input.readLine();
                    if (message.equals("0000000")) {
                        JOptionPane.showMessageDialog(jf, "该房间已满员！", "Error", JOptionPane.INFORMATION_MESSAGE);
                        break;
                    }
                    if (message.startsWith("MATCHED")) {
                        String[] tokens = message.split(":");
                        System.out.println("You are now matched with " + tokens[1] + ":" + tokens[2]);
                        waiting = false;
                    } else {
                        serverReply = message;
                        System.out.println(message);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    // 对局中向对方传递信息
    public void sendMessage(String msg) {
        sendMsg = msg;
        if (waiting) {
            System.out.println("You are still waiting for a match.");
        } else {
            output.println(sendMsg);
        }
    }

    // 先手方向后手方传递盘面
    public void sendA(int[][] a, int c, int r) {
        System.out.println("sendA called");
        sendMsg = "Array<>";
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                sendMsg += "" + a[i][j] + "<>";
            }
        }
        if (waiting) {
            System.out.println("You are still waiting for a match.");
        } else {
            System.out.println(sendMsg);
            output.println(sendMsg);
        }
    }

    // 后手方接收盘面
    public void recArray() {
        mine = new int[row][column];
        new Thread(() -> {
            try {
                while (waitingArray) {
                    String message = input.readLine();
                    if (message.startsWith("Array")) {
                        String[] tokens = message.split("<>");
                        for (int i = 0; i < row; i++) {
                            for (int j = 0; j < column; j++) {
                                mine[i][j] = Integer.parseInt(tokens[1 + i * column + j]);
                            }
                        }
                        waitingArray = false;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public int[][] getMine() {
        return mine;
    }

    public boolean isWaitingArray() {
        return waitingArray;
    }

    // 对局中接收对方信息
    public void startRec() {
        new Thread(() -> {
            try {
                while (true) {
                    String message = input.readLine();
                    recMsg = message;
                    if (message != null)
                        System.out.println(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void closeSocket() throws IOException {
        socket.close();
    }

    public boolean getWaiting() {
        return waiting;
    }

    public String getServerReply() {
        return serverReply;
    }

    public String getRecMsg() {
        return recMsg;
    }
}
